// pages/submission_should_get/submission_should_get.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        successShow:false,
        tabIndex:0,
        tabList:[
            '全部',
            "待付款",
            "待发货",
            "待收货",
            "待评价",
        ],
        getIndex1:-1,
        getIndex2:-1,
        allList: [],
        shouldPayList:[],
        shouldSendList: [],
        shouldGetList: [],
        shouldCommentList: [],
    },
    chooseTabIndex:function(e){
      var that=this;
      var index=e.currentTarget.id;
      that.setData({
        tabIndex:index,
        allList:[],
      })
      that.getOrder();
    },
    phoneCall1:function(){
        var that=this;
        var phone = that.data.allList[that.data.getIndex1].telephone;
        wx.makePhoneCall({
            phoneNumber: phone,
        })
    },
    getIndex1:function(e){
        var index=e.currentTarget.id;
        var that=this;
        that.setData({
            getIndex1:index,
        })
       
    },
    //联系客服
    phoneCall2: function (e) {
      var that = this;
      var phone = e.currentTarget.dataset.telephone;
      console.log(phone)
        wx.makePhoneCall({
            phoneNumber: phone,
        })
    },
    getIndex2: function (e) {
        var index = e.currentTarget.id;
        var that = this;
        that.setData({
            getIndex2: index,
        })
    },
    //取消订单
    cancel:function(e){
        var that=this;
        var id=e.currentTarget.id;//要取消的订单的id
        var index=e.currentTarget.dataset.index;//要取消的订单的下标
        var allList = that.data.allList;
        wx.showModal({
            title: '',
            content: '确认要取消该订单吗',
            success:function(res){
                if(res.cancel){
                    //console.log("暂时不取消")
                }else{
                  wx.request({
                    url: Host + 'Mine/cancleorder',
                    data: { orderid: id},
                    success: function (res) {
                      //console.log(res.data);
                      if (res.data.code == 200) {
                        allList.splice(index, 1);
                        that.setData({
                          allList: allList,
                        })
                      } else {
                        wx.showToast({
                          title: res.data.errmsg,
                          icon: 'none',
                        })
                      }
                    }
                  })                 
                }
            }
        })
    },
  //去付款
  payorder: function (e) {
    var that = this;
    var out_trade_no = e.currentTarget.id;//订单号
    var openid = wx.getStorageSync('openid');

    wx.request({
      url: Host + 'Pay/order_zhifu',
      data: { out_trade_no, openid },
      method: 'get',
      success: function (res) {
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'none',//  loading/success/none        
            duration: 2000
          })
        } else {
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            'success': function (res) {
              //console.log(res);
              wx.redirectTo({
                url: '/pages/submission_should_get/submission_should_get?id=2'
              })
            },
            'fail': function (res) {
              //console.log(res);
            }
          })
        }
      }
    })
  },
    //确认收货
    sure:function(e){
        var that=this;
        var id = e.currentTarget.id;//确认收货的订单的id
        var index = e.currentTarget.dataset.index;//确认收货的订单的下标
        var shouldGetList = that.data.shouldGetList;
        var shouldCommentList = that.data.shouldCommentList;
        console.log(shouldCommentList)
        wx.showModal({
            title: '',
            content: '确认已收到该商品了吗',
            success:function(res){
                if(res.cancel){
                    //console.log("暂时没收到")
                }else{
                  wx.request({
                    url: Host + 'Mine/receiveorder',
                    data: { orderid: id },
                    success: function (res) {
                      //console.log(res.data);
                      if (res.data.code == 200) {
                        wx.showToast({
                          title: '确认成功',
                          icon: 'success',
                        })
                        setTimeout(function () {
                          wx.navigateTo({
                            url: '/pages/submission_should_get/submission_should_get?id=4',
                          })
                        }, 1000)
                      } else {
                        wx.showToast({
                          title: res.data.errmsg,
                          icon: 'none',
                        })
                      }
                    }
                  })  
                }
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options.id)
        if(options.id){
            this.setData({
                tabIndex: options.id,
            })
        }
        console.log(333)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this;
      that.getOrder();
    },
    getOrder:function(){
      var that = this;
      var tabIndex = that.data.tabIndex;
      var openid = wx.getStorageSync('openid');
      wx.request({
        url: Host + 'Mine/order',
        data: { index: tabIndex, openid },
        success: function (res) {
          //console.log(res.data);
          if (res.data.code == 200) {
            that.setData({
              allList: res.data.list
            })
          }
        },
        complete: function () {
          if (that.data.allList) {
            that.setData({
              successShow: true,
            })
          }
        }
      })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})